package org.sith.taskrunner.gwt.client;


import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.*;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import org.sith.taskrunner.gwt.client.dataloader.LoadInitDataStateHolder;
import org.sith.taskrunner.gwt.client.event.*;
import org.sith.taskrunner.gwt.client.presenter.*;
import org.sith.taskrunner.gwt.client.services.user.UsersManageGWTService;
import org.sith.taskrunner.gwt.client.view.*;
import org.sith.taskrunner.gwt.shared.proxy.UserEntityProxy;

import java.util.List;


public class AppController implements Presenter, ValueChangeHandler<String> {


    private EventBus eventBus;
    private HasWidgets container;
    private DataLoadPresenter dataLoadPresenter;
    private MainWidget mainWidget;

    public AppController(EventBus eventBus) {

        this.eventBus = eventBus;
        DataLoadWidget view = new DataLoadWidget();
        LoadInitDataStateHolder stateHolder = new LoadInitDataStateHolder();
        dataLoadPresenter = new DataLoadPresenter(view, stateHolder, eventBus);
        view.setPresenter(dataLoadPresenter);


        bind();
    }

    private void bind() {
        History.addValueChangeHandler(this);

        eventBus.addHandler(LoadInitDataEvent.TYPE, new LoadInitDataEventHandler() {
            @Override
            public void onInitDataLoaded(LoadInitDataEvent event) {
                mainWidget = new MainWidget();
                RootLayoutPanel.get().add(mainWidget);
                AdminButtonsWidget adminButtonsWidget = new AdminButtonsWidget();
                AdminButtonsPresenter adminButtonsPresenter = new AdminButtonsPresenter(adminButtonsWidget, eventBus);
                adminButtonsWidget.setPresenter(adminButtonsPresenter);
                adminButtonsPresenter.go(mainWidget.getRightPanel());
            }
        });

        eventBus.addHandler(OpenTaskViewEvent.TYPE, new OpenTaskViewEventHandler() {
            @Override
            public void onOpenTaskView(OpenTaskViewEvent event) {
                //To change body of implemented methods use File | Settings | File Templates.
            }
        });

        eventBus.addHandler(GuiActionEvent.TYPE, new GuiActionEventHandler() {
            @Override
            public void showUsers(GuiActionEvent event) {

                UserTableWidget view = new UserTableWidget();

                UsersTablePresenter usersTablePresenter = new UsersTablePresenter(view, eventBus);
                view.setPresenter(usersTablePresenter);

                VerticalPanel content = new VerticalPanel();
                usersTablePresenter.go(content);
                mainWidget.addTabInCenterPanel(usersTablePresenter.getTabTitle(), content);
            }

            @Override
            public void showUserManage(GuiActionEvent event) {
                UserManageWidget userManageWidget = new UserManageWidget();
                UserManagePresenter userManagePresenter = new UserManagePresenter(userManageWidget, eventBus);
                userManageWidget.setPresenter(userManagePresenter);
                VerticalPanel content = new VerticalPanel();
                userManagePresenter.go(content);
                mainWidget.addTabInCenterPanel(userManagePresenter.getTabTitle(), content);


            }
        });
    }


    @Override
    public void onValueChange(ValueChangeEvent<String> stringValueChangeEvent) {
        String value = stringValueChangeEvent.getValue();
        HistoryTokens historyToken = null;
        try {
            historyToken = HistoryTokens.valueOf(value);
        } catch (IllegalArgumentException e) {
            Window.alert("Bad history token " + value);
        }

        switch (historyToken) {
            case LOAD_INIT_DATA: {
//                loadInitDataService.
            }
            break;

            case START_APPLICATION: {
                dataLoadPresenter.go(container);

            }
            break;


            default: {
                Window.alert("Unused history token " + value);
            }
        }
    }

    public void go(final HasWidgets container) {
        this.container = container;

        if ("".equals(History.getToken())) {
            History.newItem(HistoryTokens.START_APPLICATION.name());
        } else {
            History.fireCurrentHistoryState();
        }


    }
}
